﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace Win暂停更新
{
    //读写（使用）
    //string key = @"SOFTWARE\Microsoft\WindowsUpdate\UX\Settings";
    //WRregedit.WriteRegistryValue(Registry.LocalMachine, key, "PauseFeatureUpdatesStartTime", nowIsoStr);

    //RegistryValueKind.String     // REG_SZ
    //RegistryValueKind.ExpandString  // REG_EXPAND_SZ
    //RegistryValueKind.Binary     // REG_BINARY
    //RegistryValueKind.DWord      // REG_DWORD
    //RegistryValueKind.MultiString  // REG_MULTI_SZ
    //RegistryValueKind.QWord      // REG_QWORD
    internal class WRregedit
    {

        /// <summary>
        /// 创建或打开注册表项
        /// </summary>
        public static RegistryKey? CreateRegistryKey(RegistryKey rootKey, string subKeyPath, bool writable = true)
        {
            try
            {
                return rootKey.CreateSubKey(subKeyPath, writable);
            }
            catch (SecurityException ex)
            {
                Console.WriteLine($"[安全错误] 无权限创建注册表项: {ex.Message}");
            }
            catch (UnauthorizedAccessException ex)
            {
                Console.WriteLine($"[访问拒绝] 无法访问注册表: {ex.Message}");
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine($"[参数错误] 无效路径: {ex.Message}");
            }
            return null;
        }
        /// <summary>
        /// 写入注册表值（会自动创建不存在的项）
        /// </summary>
        public static bool WriteRegistryValue(RegistryKey rootKey, string subKeyPath,
                                            string valueName, object value,
                                            RegistryValueKind valueKind = RegistryValueKind.String)
        {
            try
            {
                using (var key = CreateRegistryKey(rootKey, subKeyPath))
                {
                    if (key != null)
                    {
                        key.SetValue(valueName, value, valueKind);
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[写入失败] {ex.Message}");
            }
            return false;
        }
        /// <summary>
        /// 读取注册表值
        /// </summary>
        public static object? ReadRegistryValue(RegistryKey rootKey, string subKeyPath, string valueName, object? defaultValue = null)
        {
            try
            {
                using (var key = rootKey.OpenSubKey(subKeyPath))
                {
                    if (key != null)
                    {
                        return key.GetValue(valueName, defaultValue);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[读取失败] {ex.Message}");
            }
            return defaultValue;
        }
        /// <summary>
        /// 删除注册表值
        /// </summary>
        public static bool DeleteRegistryValue(RegistryKey rootKey, string subKeyPath, string valueName)
        {
            try
            {
                using (var key = rootKey.OpenSubKey(subKeyPath, true))
                {
                    if (key != null)
                    {
                        // 检查值是否存在
                        if (key.GetValue(valueName) != null)
                        {
                            key.DeleteValue(valueName);
                            return true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[删除失败] {ex.Message}");
            }
            return false;
        }
        /// <summary>
        /// 递归删除整个注册表项
        /// </summary>
        public static bool DeleteRegistryKeyTree(RegistryKey rootKey, string subKeyPath)
        {
            try
            {
                // 直接删除整个子树（包括所有子项）
                rootKey.DeleteSubKeyTree(subKeyPath, true);
                return true;
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine($"[路径错误] {ex.Message}");
            }
            catch (SecurityException ex)
            {
                Console.WriteLine($"[安全错误] 权限不足: {ex.Message}");
            }
            return false;
        }
        /// <summary>
        /// 检查注册表项是否存在
        /// </summary>
        public static bool RegistryKeyExists(RegistryKey rootKey, string subKeyPath)
        {
            using (var key = rootKey.OpenSubKey(subKeyPath))
            {
                return key != null;
            }
        }
        /// <summary>
        /// 检查注册表值是否存在
        /// </summary>
        public static bool RegistryValueExists(RegistryKey rootKey, string subKeyPath, string valueName)
        {
            using (var key = rootKey.OpenSubKey(subKeyPath))
            {
                if (key != null)
                {
                    return key.GetValue(valueName) != null;
                }
            }
            return false;
        }


















    }
}
